﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class ListViewForm : Form, ViewForm
    {
        filtr myfiltr = filtr.all;
        Form1 par;
        int size;
        public Form1.addDataHandler aH;
        public Form1.replaceDataHandler rpH;
        public Form1.removeDataHandler rmH;
        public delegate void filtrChangedHandler(ViewForm vf);
        public event filtrChangedHandler filtrChangedEvent;

        public delegate void replaceShowHandler(Data d);
        public event replaceShowHandler replaceShowEvent;
        public delegate void removeShowHandler(Data d);
        public event removeShowHandler removeShowEvent;

        public ListViewForm(Form1 _par)
        {
            InitializeComponent();
            all.Checked = true;
            size = 0;
            par = _par;
            filtrChangedEvent += new filtrChangedHandler(par.filtrChanged);
            replaceShowEvent += new replaceShowHandler(par.replaceShow);
            removeShowEvent += new removeShowHandler(par.removeShow);
        }

        bool acceptData(Data data)
        {

            if (!(myfiltr == filtr.all ||
                (myfiltr == filtr.smaller && data.x < 0) ||
                (myfiltr == filtr.bigger && data.x >= 0)))
                return false;
            return true;
        }

        public void addItemData(Data data)
        {
            if (!acceptData(data))
                return;
            ListViewItem item = new ListViewItem(data.label);
            item.Tag = data;
            item.SubItems.Add((data.x).ToString());
            item.SubItems.Add((data.y).ToString());
            item.SubItems.Add((data.color).ToString());
            item.SubItems[3].ForeColor = ColorControl.getRGB(data.color);
            listView1.Items.Add(item);
            size++;
            StatusLabel1.Text = size.ToString();

        }

        public void removeItemData(Data data)
        {
            foreach (ListViewItem it in listView1.Items)
            {
                if ((Data)it.Tag == data)
                {
                    Console.WriteLine("vremove item list");
                    listView1.Items.Remove(it);
                    size--;
                    StatusLabel1.Text = size.ToString();
                    break;
                }
            }
        }
        public void replaceItemData(Data d)
        {
            bool found = false;
            Console.WriteLine("replace enent");
            foreach (ListViewItem it in listView1.Items)
            {
                Console.WriteLine("vnt hand reo");
                if ((Data)it.Tag == d)
                {
                    found = true;
                    if (!acceptData(d))
                    {
                        removeItemData(d);
                    }
                    else
                    {
                        it.Text = d.label;
                        it.SubItems[1].Text = d.x.ToString();
                        it.SubItems[2].Text = d.y.ToString();
                        it.SubItems[3].Text = d.color.ToString();
                    }
                }
            }
            if (!found && acceptData(d))
            {
                addItemData(d);
            }
        }

       

        private void ListViewForm_Load(object sender, EventArgs e)
        {

        }
        public ToolStrip ChildToolStrip 
        {
            get { return toolStrip1; }
            set { toolStrip1 = value; }
        }

        public StatusStrip ChildStatusStrip
        {
            get { return statusStrip1; }
            set { statusStrip1 = value; }
        }

        private void all_Click(object sender, EventArgs e)
        {
            setFiltr(filtr.all);
        }

        private void small_Click(object sender, EventArgs e)
        {
            setFiltr(filtr.smaller);
        }

        private void big_Click(object sender, EventArgs e)
        {
            setFiltr(filtr.bigger);
        }
        public void setFiltr(filtr f)
        {
            if (myfiltr == f)
                return;
            switch (f)
            {
                case filtr.all:
                    myfiltr = filtr.all;
                    all.Checked = true;
                    small.Checked = false;
                    big.Checked = false;
                    break;
                case filtr.smaller:
                    myfiltr = filtr.smaller;
                    all.Checked = false;
                    small.Checked = true;
                    big.Checked = false;
                    break;
                case filtr.bigger:
                    myfiltr = filtr.bigger;
                    all.Checked = false;
                    small.Checked = false;
                    big.Checked = true;
                    break;
            }
            listView1.Items.Clear();
            size = 0;
            StatusLabel1.Text = size.ToString();
            filtrChangedEvent(this);

        }

        private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            //if (e.Button == MouseButtons.Right)
            
                Console.WriteLine("_MouseClick");
               ListViewItem item = listView1.GetItemAt(e.X, e.Y);
               if (listView1.SelectedItems.Count > 0)
               {
                   replaceToolStripMenuItem.Enabled = true;
                   removeToolStripMenuItem.Enabled = true;
               }
               else
               {

                   replaceToolStripMenuItem.Enabled = false;
                   removeToolStripMenuItem.Enabled = false;
               
               }
                //contextMenuStrip1.Show(listView1, e.X, e.Y);
            
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {

            Data d = (Data)listView1.SelectedItems[0].Tag;
            replaceShowEvent(d);
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Console.WriteLine("SelectedIndexChanged");
            if (listView1.SelectedItems.Count > 0)
            {
                toolStripButtonRemove.Enabled = true;
                toolStripButtonReplace.Enabled = true;
                removeToolStripMenuItem.Enabled = true;
                replaceToolStripMenuItem.Enabled = true;
                removeToolStripMenuItem1.Enabled = true;
                replaceToolStripMenuItem1.Enabled = true;
                Console.WriteLine("not null");
            }
            else
            {

                toolStripButtonRemove.Enabled = false;
                toolStripButtonReplace.Enabled = false;
                replaceToolStripMenuItem.Enabled = false;
                removeToolStripMenuItem.Enabled = false;
                replaceToolStripMenuItem1.Enabled = false;
                removeToolStripMenuItem1.Enabled = false;
                Console.WriteLine(" null");
            }
        
        }

        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ;
            Data d = (Data)listView1.SelectedItems[0].Tag;
            removeShowEvent(d);
        }

        private void ListViewForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!par.closeChild(this))
                e.Cancel = true;
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {

            Console.WriteLine("ItemSelectionChangedd");
            if (listView1.SelectedItems.Count>0)
            {
                toolStripButtonRemove.Enabled = true;
                toolStripButtonReplace.Enabled = true;
                removeToolStripMenuItem.Enabled = true;
                replaceToolStripMenuItem.Enabled = true;
                removeToolStripMenuItem1.Enabled = true;
                replaceToolStripMenuItem1.Enabled = true;
                Console.WriteLine("not null");
            }
            else
            {

                toolStripButtonRemove.Enabled = false;
                toolStripButtonReplace.Enabled = false;
                replaceToolStripMenuItem.Enabled = false;
                removeToolStripMenuItem.Enabled = false;
                replaceToolStripMenuItem1.Enabled = false;
                removeToolStripMenuItem1.Enabled = false;
                Console.WriteLine(" null");
            }
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            par.addToolStripMenuItem_Click(sender, e);
        }

        private void ListViewForm_FormClosed(object sender, FormClosedEventArgs e)
        {

            par.addDataEvent -= aH;
            par.removeDataEvent -= rmH;
            par.replaceDataEvent -= rpH;
        }
    }
}
